<?php
include("../lib/adodb/adodb-exceptions.inc.php");
include("../lib/adodb/adodb.inc.php");

/**
* This class avoid all transactions between classes and DB, this DBInterface it's exclusive for PostgresQL
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
*/
class DBInterface
{
    private $conn;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->conn = &NewADOConnection('postgres');
    }

    /**
    * Create a persistent connection to DB
    */
    function connect()
    {
        try
        {
//             $this->conn->PConnect('localhost','juangber','juangber','www');
            $this->conn->PConnect('localhost','charagmz','shirley','www');
            $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);
        }
        catch(exception $e)
        {
            print_r($e);
        }
    }

    /**
    * Execute a SELECT query
    * @param Array $fields An array with the field names to be retrieve
    * @param Mixed $table Name of the table, or an array of table names
    * @param String $criteria Criteria for the query, this is optional
    * @param String $other If the query has a sentence like order by but without criteria, this is optional
    * @param Bool $distinct Flag if the SELECT query have the restriction DISTINCT, this is optional
    * @return Array Bidimensional array with query results
    */
    function exec_select($fields,$table,$criteria='',$other='',$distinct=false)
    {
        if($distinct)
            $sql = "select distinct ";
        else
            $sql = "select ";
        $sql .= implode(",",$fields)." from ";
        if(is_array($table))
            $sql .= implode(",",$table);
        else
            $sql .= $table;
        if($criteria!='')
            $sql .= " where ".$criteria;
        if($other!='')
            $sql .= " ".$other;

//         echo $sql;

        $this->connect();
        $result = $this->conn->_Execute($sql);

        if($result->RecordCount()==0)
            return 0;
        else
            return $result->getArray();
    }

    /**
    * Execute a INSERT query
    * @param Array $data An associative array where the key is the field name and the value is the field value
    * @param String $table Name of the table
    * @return Bool Indicates if the query was succesful
    */
    function exec_insert($data,$table)
    {
        $fld_names = array();
        $fld_values = array();
        foreach($data as $key => $value)
        {
            $fld_names[] = $key;
            $fld_values[] = $value;
        }
        $i=0;
        foreach($fld_values as $value)
        {
            $fld_values[$i] = "'".str_replace("'","''",$value)."'";
            $i++;
        }

        $sql = "insert into ".$table." (".implode(",",$fld_names).") values (".implode(",",$fld_values).")";

        $this->connect();
        if($this->conn->_Execute($sql)===false)
            return false;
        else
            return true;
    }

    /**
    * Execute a UPDATE query
    * @param Array $data An associative array where the key is the field name and the value is the field value
    * @param String $table Name of the table
    * @param String $criteria Criteria for the query, this is optional
    */
    function exec_update($data,$table,$criteria='')
    {
        $fields = array();
        foreach($data as $key => $value)
        {
            $fields[] = $key."='".$value."'";
        }

        $sql = "update ".$table." set ".implode(",",$fields);
        if($criteria!='')
            $sql .= " where ".$criteria;

        $this->connect();
        try
        {
            $result = $this->conn->_Execute($sql);
            return true;
        }
        catch(exception $e)
        {
            return false;
        }
    }

    /**
    * Execute a DELETE query
    * @param String $table Name of the table
    * @param String $criteria Criteria for the query, this is optional
    */
    function exec_delete($table,$criteria='')
    {
        $sql = "delete from ".$table;
        if($criteria!='')
            $sql .= " where ".$criteria;

        $this->connect();
        $result = $this->conn->_Execute($sql);
    }

    /**
    * Execute a particular query given by user
    * @param String $sql The query
    */
    function exec_sql($sql)
    {
        $result = $this->conn->_Execute($sql);
    }
}
?>